package com.mes.service.manu;

import com.yy.base.common.dao.Dao;
import com.yy.base.common.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class CooperativeVehicleService extends BaseService {
	@Autowired
	private Dao dao;


	//根据RFID带出对应明细信息
	public List<Map<String,Object>> getPhotoListsRight(String code) {

		String sql = "SELECT  " +
				"  zy.CODE AS ZYCODE,  " +
				"  wl.NAME AS WLNAME,  " +
				"  bw.NAME AS BWNAME,  " +
				"  jh.color as YSNAME,  " +
				"  bt.pic_file_id AS LEFTIMG,  " +
				"  bt.data_file_id AS RIGHTIMG,  " +
				"  zygx.STATUS AS ZYSTATUS,  " +
				"  ( SELECT NAME FROM m_product WHERE m_product_id = bom.jiaban_id ) AS JBNAME,  " +
				"  zygx.route_no - 10 AS nums,  " +
				"  ( SELECT a.STATUS FROM Emeso_Job_Order_Exe a WHERE a.route_no = zygx.route_no - 10 AND a.emeso_job_order_id = zy.Emeso_Job_Order_id ) AS statuss,  " +
				"  ( SELECT a.emeso_job_order_exe_id FROM Emeso_Job_Order_Exe a WHERE a.route_no = zygx.route_no - 10 AND a.emeso_job_order_id = zy.Emeso_Job_Order_id ) AS exeid,  " +
				"  zygx.Emeso_Job_Order_Exe_id AS EXEIDS ,  " +
				"  (select a.code from emes_size a where a.emes_size_id = bt.emes_size_id ) as SIZECODE  " +
				"FROM  " +
				"  Emeso_Job_Order zy,  " +
				"  Emeso_Job_Order_Exe zygx,  " +
				"  emes_process gx,  " +
				"  emes_process_component gxbw,  " +
				"  Emes_Pbom_Item bwitem,  " +
				"  emes_pbom bom,  " +
				"  emes_processopr gxxx,  " +
				"  m_product wl,  " +
				"  Emes_Component bw,  " +
				"  Emes_PBom_Item_File bt ,  " +
				"  Emeso_Job_Plan zyj,  " +
				"  emeso_manuorder sc,  " +
				"  emesp_planorder jh  " +
				"WHERE  " +
				"  zy.emeso_job_order_id = zygx.emeso_job_order_id   " +
				"  AND zygx.emes_process_id = gx.emes_process_id   " +
				"  and zy.emeso_manuorder_id = sc.emeso_manuorder_id  " +
				"  and sc.emesp_planorder_id = jh.emesp_planorder_id  " +
				"  AND gx.emes_processopr_id = gxxx.emes_processopr_id   " +
				"  AND gx.emes_process_id = gxbw.emes_process_id   " +
				"  AND gxbw.emes_pbom_item_id = bwitem.emes_pbom_item_id   " +
				"  AND bwitem.emes_component_id = bw.emes_component_id   " +
				"  AND bwitem.emes_pbom_id = bom.emes_pbom_id   " +
				"  AND zy.m_product_id = wl.m_product_id   " +
				"  AND bt.emes_pbom_item_id = bwitem.emes_pbom_item_id   " +
				"  and zy.emeso_manuorder_id = zyj.emeso_manuorder_id  " +
				"  and bt.emes_size_id = zyj.emes_size_id  " +
				"  and zyj.type = 'RIGHT'  " +
				"  and bt.data_file_id is not null  " +
				"  AND gxxx.CODE = 'XIEZUOCHE'   " +
				"  AND zy.code = ? ";


		return dao.query(sql, code );
	}

	//根据RFID带出对应明细信息
	public List<Map<String,Object>> getPhotoListsLeft(String code) {

		String sql = "SELECT  " +
				"  zy.CODE AS ZYCODE,  " +
				"  wl.NAME AS WLNAME,  " +
				"  bw.NAME AS BWNAME,  " +
				"  jh.color as YSNAME,  " +
				"  bt.pic_file_id AS LEFTIMG,  " +
				"  bt.data_file_id AS RIGHTIMG,  " +
				"  zygx.STATUS AS ZYSTATUS,  " +
				"  ( SELECT NAME FROM m_product WHERE m_product_id = bom.jiaban_id ) AS JBNAME,  " +
				"  zygx.route_no - 10 AS nums,  " +
				"  ( SELECT a.STATUS FROM Emeso_Job_Order_Exe a WHERE a.route_no = zygx.route_no - 10 AND a.emeso_job_order_id = zy.Emeso_Job_Order_id ) AS statuss,  " +
				"  ( SELECT a.emeso_job_order_exe_id FROM Emeso_Job_Order_Exe a WHERE a.route_no = zygx.route_no - 10 AND a.emeso_job_order_id = zy.Emeso_Job_Order_id ) AS exeid,  " +
				"  zygx.Emeso_Job_Order_Exe_id AS EXEIDS ,  " +
				"  (select a.code from emes_size a where a.emes_size_id = bt.emes_size_id ) as SIZECODE  " +
				"FROM  " +
				"  Emeso_Job_Order zy,  " +
				"  Emeso_Job_Order_Exe zygx,  " +
				"  emes_process gx,  " +
				"  emes_process_component gxbw,  " +
				"  Emes_Pbom_Item bwitem,  " +
				"  emes_pbom bom,  " +
				"  emes_processopr gxxx,  " +
				"  m_product wl,  " +
				"  Emes_Component bw,  " +
				"  Emes_PBom_Item_File bt ,  " +
				"  Emeso_Job_Plan zyj,  " +
				"  emeso_manuorder sc,  " +
				"  emesp_planorder jh  " +
				"WHERE  " +
				"  zy.emeso_job_order_id = zygx.emeso_job_order_id   " +
				"  AND zygx.emes_process_id = gx.emes_process_id   " +
				"  and zy.emeso_manuorder_id = sc.emeso_manuorder_id  " +
				"  and sc.emesp_planorder_id = jh.emesp_planorder_id  " +
				"  AND gx.emes_processopr_id = gxxx.emes_processopr_id   " +
				"  AND gx.emes_process_id = gxbw.emes_process_id   " +
				"  AND gxbw.emes_pbom_item_id = bwitem.emes_pbom_item_id   " +
				"  AND bwitem.emes_component_id = bw.emes_component_id   " +
				"  AND bwitem.emes_pbom_id = bom.emes_pbom_id   " +
				"  AND zy.m_product_id = wl.m_product_id   " +
				"  AND bt.emes_pbom_item_id = bwitem.emes_pbom_item_id   " +
				"  and zy.emeso_manuorder_id = zyj.emeso_manuorder_id  " +
				"  and bt.emes_size_id = zyj.emes_size_id  " +
				"  and zyj.type = 'LEFT'  " +
				"  and bt.pic_file_id is not null  " +
				"  AND gxxx.CODE = 'XIEZUOCHE'   " +
				"  AND zy.code = ? ";


		return dao.query(sql, code );
	}

	//根据RFID找到对应的夹板编码
	public List<Map<String,Object>> getProductJbLists(String code) {

		String sql = "  select item.code as RFIDCODE , bom.jiaban_id as WLID " +
				"  from emeso_job_order zy  ,emes_pbom bom , emes_size cm , emeso_manuorder sc ,wms_storage_batch batch , wms_storage_batch_item item  " +
				"  where zy.m_product_id = bom.m_product_id  " +
				"  and zy.emeso_manuorder_id = sc.emeso_manuorder_id  " +
				"  and sc.foot_size_code = cm.code  " +
				"  and batch.m_product_id = bom.jiaban_id  " +
				"  and batch.emes_size_id = cm.emes_size_id  " +
				"  and batch.wms_storage_batch_id = item.wms_storage_batch_id  " +
				"  and zy.code = ? ";


		return dao.query(sql, code );
	}

	//根据物料ID查询对应仓库
	public List<Map<String,Object>> selectWarehouseCode(String id) {
		String sql = " select ck.shipper_code as CKCODE from wms_warehouse_product wl , m_warehouse ck  " +
				" where wl.m_warehouse_id = ck.m_warehouse_id " +
				" and wl.m_product_id = ? " ;

		return dao.query(sql,id);
	}
	//修改工序的执行状态开工
	@SuppressWarnings("unchecked")
	public boolean updateExeBeginStatus(String Ids,String status) {
		boolean result = false;


		String sql =  "UPDATE Emeso_Job_Order_Exe SET status = ?, Exe_Date_From = now()   WHERE  Emeso_Job_Order_Exe_id = ? ";

		result = dao.executeSql(sql, status,Ids) > 0;

		return result;
	}

	//修改工序的执行状态为完工
	@SuppressWarnings("unchecked")
	public boolean updateExeEndStatus(String Ids,String status) {
		boolean result = false;


		String sql =  "UPDATE Emeso_Job_Order_Exe SET status = ?, Exe_Date_To = now()   WHERE  Emeso_Job_Order_Exe_id = ? ";

		result = dao.executeSql(sql, status,Ids) > 0;

		return result;
	}

}